package com.concurrent.chapter6.concurrent01;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Run1 {
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newCachedThreadPool();
		CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
		completionService.submit(new Callable<String>() {
			
			@Override
			public String call() throws Exception {
				TimeUnit.SECONDS.sleep(3);
				System.out.println("3秒过了");
				return "3S";
			}
		});
		//poll()方法获取并移除表示下一个已完成任务的Future，
		//如果不存在这样的任务则返回null，无阻塞效果
		System.out.println(completionService.poll());
	}
}
